# init.py  ———— 替换全文
from config import LOG_DIR, CONFIG_DIR, REAGENT_FILE, INJECT_FILE, SYSTEM_STATE_FILE
from pathlib import Path
import json

def create_directories():
    Path(LOG_DIR).mkdir(parents=True, exist_ok=True)
    Path(CONFIG_DIR).mkdir(parents=True, exist_ok=True)

def create_default_configs():
    if not REAGENT_FILE.exists():
        with open(REAGENT_FILE, 'w', encoding='utf-8') as f:
            json.dump([{"port": i, "reagent": ""} for i in range(1, 10)], f, ensure_ascii=False, indent=2)

    if not INJECT_FILE.exists():
        with open(INJECT_FILE, 'w', encoding='utf-8') as f:
            json.dump(
                [{"index": i, "reagent": "", "cycles": 1, "timewait": 10, "temperature": 25} for i in range(1, 8)],
                f, ensure_ascii=False, indent=2
            )

    if not SYSTEM_STATE_FILE.exists():
        with open(SYSTEM_STATE_FILE, 'w', encoding='utf-8') as f:
            json.dump({
                "device_status": "idle",
                "temperature": 25.0,
                "current_reagent_config": None,
                "current_inject_config": None,
                "reagent_locked": False,
                "reaction_paused": False,
                "current_step_index": 0,
                "current_cycle_count": 0,
                "current_step_info": "",
                "next_action_info": ""
            }, f, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    create_directories()
    create_default_configs()
    print("系统初始化完成")
